set more off

use dataset, clear

keep if abs(x) < 100

local nreps 500
local covariates pctwhite pct65plus pcths lmedianincome logvap2014 pctui2013
 
local Z pcteligible2013

su `Z', det
gen Z = `Z' >=r(p50)
replace Z = . if mi(`Z')

gen Zx = Z*x
gen Zz = Z*z
gen Zzx = Z*zx

foreach x in z Z Zz {
	gen `x'B = ""
	gen `x'CI = ""
}
foreach x in z Z Zz {
	gen `x'N = ""
	gen `x'Ns = ""
	gen `x'bw = ""
	gen `x'cov = ""
	gen `x'rsq = ""
}

gen index = _n

* programs to store estimates (eststo doesn't work well with cgmboot)
capture program drop myest1
program define myest1
	args i bw cov
	mat bootresults = e(bootresults)
	svmat bootresults
	local j = 4
	foreach x in z Z Zz {
		replace `x'B = string(_b[`x'], "%20.02fc") if index == `i'
		_pctile bootresults`j' if bootresults3 == 0, nq(1000)
		replace `x'CI = "[" + string(r(r25), "%20.02fc") + ", " + string(r(r975), "%20.02fc") + "]" if index == `i'
		if `j' == 4 {
			replace `x'N = string(e(N), "%20.00fc") if index == `i'
			replace `x'Ns = string(e(N_stateabbr), "%20.00fc") if index == `i'
			replace `x'bw = "`bw'" if index == `i'
			replace `x'cov = "`cov'" if index == `i' 
			replace `x'rsq = string(e(r2), "%20.02fc") if index == `i'
		}
		local j = `j' + 1
	}	
	drop bootresults*

end

su demvoteshare, det
local mymed = r(p50)

preserve

	keep if demvoteshare < `mymed'
	
	drop index
	gen index = _n

	cgmwildboot dpctui20142013 z Z Zz x zx Zx Zzx if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
	myest1 1 "100 Miles" "No"

	cgmwildboot dpctui20142013 z Z Zz x zx Zx Zzx `covariates' if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
	myest1 2 "100 Miles" "Yes"

	cgmwildboot dpctui20152013 z Z Zz x zx Zx Zzx if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
	myest1 3 "100 Miles" "No"

	cgmwildboot dpctui20152013 z Z Zz x zx Zx Zzx `covariates' if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
	myest1 4 "100 Miles" "Yes"

	* output

	keep if index < 5
	keep *B *CI* *N *Ns *bw *cov *rsq
	l

	sxpose, clear force

	gen rowname = ""
	replace rowname = "Medicaid expansion" if _n == 1
	replace rowname = "High eligibility" if _n == 3
	replace rowname = "Expansion X eligibility" if _n == 5
	replace rowname = "Number of Counties" if _n == 7
	replace rowname = "Number of States" if _n == 8
	replace rowname = "Window" if _n == 9
	replace rowname = "Covariates" if _n == 10
	replace rowname = "R-squared" if _n == 11
	order rowname 
	keep if _n <= 11

	#delimit;

	listtex 
		using "_output/tableA16.tex"
		,
		replace
		type rstyle(tabular)
		head(
			\begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{5}{c}}
			\toprule &
			\multicolumn{2}{c}{\underline{Percent uninsured}} & 
			\multicolumn{2}{c}{\underline{Percent uninsured}}  \\
			& \multicolumn{2}{c}{\underline{2014-2013}} &
			\multicolumn{2}{c}{\underline{2015-2014}}  \\
			&(1)&(2)&(3)&(4)\\
			\midrule
		)
		foot("\bottomrule\end{tabular*}")
		;
		
	#delimit cr

	l

restore

preserve

	keep if demvoteshare > `mymed'
	
	drop index
	gen index = _n

	cgmwildboot dpctui20142013 z Z Zz x zx Zx Zzx if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
	myest1 1 "100 Miles" "No"

	cgmwildboot dpctui20142013 z Z Zz x zx Zx Zzx `covariates' if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
	myest1 2 "100 Miles" "Yes"

	cgmwildboot dpctui20152013 z Z Zz x zx Zx Zzx if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
	myest1 3 "100 Miles" "No"

	cgmwildboot dpctui20152013 z Z Zz x zx Zx Zzx `covariates' if abs(x) < 100, cl(stateabbr) bootcluster(stateabbr) reps(`nreps')
	myest1 4 "100 Miles" "Yes"

	* output

	keep if index < 5
	keep *B *CI* *N *Ns *bw *cov *rsq
	l

	sxpose, clear force

	gen rowname = ""
	replace rowname = "Medicaid expansion" if _n == 1
	replace rowname = "High eligibility" if _n == 3
	replace rowname = "Expansion X eligibility" if _n == 5
	replace rowname = "Number of Counties" if _n == 7
	replace rowname = "Number of States" if _n == 8
	replace rowname = "Window" if _n == 9
	replace rowname = "Covariates" if _n == 10
	replace rowname = "R-squared" if _n == 11
	order rowname 
	keep if _n <= 11

	#delimit;

	listtex 
		using "_output/tableA17.tex"
		,
		replace
		type rstyle(tabular)
		head(
			\begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{5}{c}}
			\toprule &
			\multicolumn{2}{c}{\underline{Percent uninsured}} & 
			\multicolumn{2}{c}{\underline{Percent uninsured}}  \\
			& \multicolumn{2}{c}{\underline{2014-2013}} &
			\multicolumn{2}{c}{\underline{2015-2014}}  \\
			&(1)&(2)&(3)&(4)\\
			\midrule
		)
		foot("\bottomrule\end{tabular*}")
		;
		
	#delimit cr

	l

restore
